<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/12/2
 * Time: 12:16
 */
namespace app\models;

use think\Model;

/**
 * Class TeacherModel
 * @package app\models
 */
class ClassRoomModel extends Model
{
    protected $table = 'edu_class_room';

    /**
     * @param $teacherDepartment
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getByDepartment($teacherDepartment)
    {
        if (!$teacherDepartment) {
            return [];
        }

        return self::where('teacher_department', $teacherDepartment)
            ->field('teacher_name')
            ->select();
    }

    /**
     * @param $teacherGrade
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getByGrade($teacherGrade)
    {
        if (!$teacherGrade) {
            return [];
        }

        return self::where('teacher_grade', $teacherGrade)
            ->field('teacher_name')
            ->select();
    }

    /**
     * @param $teacherSubject
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getBySubject($teacherSubject)
    {
        if (!$teacherSubject) {
            return [];
        }

        return self::where('teacher_subject', $teacherSubject)
            ->field('teacher_name')
            ->select();
    }

    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getClassRoomLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['class_name']) && '' !== $param['class_name']) {
            $where['class_name'] = ['like', $param['class_name'].'%'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                  $item['status_text'] = (1 === $item['status']) ? '启用中' : '禁用中';
                  $item['campus_id'] = CampusModel::get(['id'=>$item['campus_id']])['campus_name'];
            }
        }
        return $listData;
    }
}